﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InSys.Documents
{
    public partial class FormSelectUser : Form
    {
        private MapTestLinqDataContext db = new MapTestLinqDataContext();
        private AccessUserLoginInfo info;
        private int id_user;
        private int id_department;
        private string name_user;
        private string name_department;

        public int IdUser { get { return id_user; } }
        public int IdDepartment { get { return id_department; } }
        public string NameUser { get { return name_user; } }
        public string NameDepartment { get { return name_department; } }
        public FormSelectUser()
        {
            InitializeComponent();
            info = new AccessUserLoginInfo();
        }

        private void listBoxDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            int value = departmentListBox.SelectedValue == null ? 0 : (int)departmentListBox.SelectedValue;
            userBindingSource.DataSource = null;
            var queryUsers = info.getListPersonsDepartment(value, Program.gWorkDate);
            userBindingSource.DataSource = queryUsers;
            id_department = value;
            name_department = departmentListBox.SelectedItem == null ? "" : (departmentListBox.SelectedItem as department).name;
            
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            id_user = userListBox.SelectedValue == null ? 0 : (int)userListBox.SelectedValue;
            name_user = userListBox.SelectedItem == null ? "" : (userListBox.SelectedItem as person).name;
            DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void FormSelectUser_Load(object sender, EventArgs e)
        {
            id_department = 0;
            id_user = 0;
            if (Properties.Settings.Default.debug > 0)
            {
                id_user = 4;
                id_department = 1;
                name_user = "Сидоров (отладка)";
                name_department = "ОРНП (отладка)";
                DialogResult = DialogResult.OK;
            }

            var queryDepartments = from item in db.departments orderby item.name select item;
            departmentBindingSource.DataSource = queryDepartments;
            userBindingSource.DataSource = null;
            selectDepartment(departmentListBox.SelectedValue == null ? 0 : (int)departmentListBox.SelectedValue, Program.gWorkDate);
        }

        /// <summary>
        /// Осуществить действия при указании номера отдела
        /// </summary>
        /// <param name="value"></param>
        private void selectDepartment(int value, DateTime actual_date)
        {
            userBindingSource.DataSource = null;
            var queryUsers = info.getListPersonsDepartment(value, actual_date);
            userBindingSource.DataSource = queryUsers;
            id_department = value;
            name_department = departmentListBox.SelectedItem == null ? "" : (departmentListBox.SelectedItem as department).name;
        }

        /// <summary>
        /// Осуществить действия при выборе сотрудника
        /// </summary>
        /// <param name="value"></param>
        private void selectUser(int value)
        {
            id_user = value;
            name_user = userListBox.SelectedItem == null ? "" : (userListBox.SelectedItem as person).name;
        }

        private void userListBox_DoubleClick(object sender, EventArgs e)
        {
            id_user = userListBox.SelectedValue == null ? 0 : (int)userListBox.SelectedValue;
            name_user = userListBox.SelectedItem == null ? "" : (userListBox.SelectedItem as person).name;
            DialogResult = DialogResult.OK;
        }

    }
}
